//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


package ants.p2p.utils.net;

import ants.p2p.NeighbourAnt;
import java.io.*;

public  class BandwidthFilterOut extends java.io.FilterOutputStream {

    //BandwidthFilterControlerOut controler;
    static int used=0;
    //int limit;


    //Constructors
    public BandwidthFilterOut(OutputStream o){
        super(o);
        //limit = 1024*10;
        //controler = new BandwidthFilterControlerOut(this, 1000);
        //controler.start();
    }
    /*public BandwidthFilterOut(OutputStream o, int bandwidth){
        super(o);
        limit = bandwidth;
        controler = new BandwidthFilterControlerOut(this, 1000);
        controler.start();
    }
    public BandwidthFilterOut(OutputStream o, int bandwidth, int timeSlotLength){
        super(o);
        limit = bandwidth;
        controler = new BandwidthFilterControlerOut(this, timeSlotLength);
        controler.start();
    }

    public void setLimit(int limit){
      this.limit = limit;
      this.used = 0;
    }*/


    /*protected synchronized void doNotify(){
        notify();
    }*/

    protected void bandwidthCheck(){
      synchronized(BandwidthFilterControlerOut.getInstance()){
        if (used >= NeighbourAnt.bandwidthLimit) {
          try {
            //System.out.println("Bandwidth Limit Reached..");
            BandwidthFilterControlerOut.getInstance().wait();
          }
          catch (Exception e) {
          }
        }
      }
    }


    //Standerd Interfaces
    public void close() throws IOException {
        //if (controler.isAlive()) controler.stop();
        out.close();
    }
    public void write(byte b[], int off, int len) throws IOException {
        byte[] array = new byte[len];
        System.arraycopy(b,off,array,0,len);
        write(array);
    }
    public void write(int b) throws IOException {
        this.bandwidthCheck();
        out.write(b);
        used++;
    }
    public void write(byte b[]) throws IOException {
        this.bandwidthCheck();
        if (b.length<=(NeighbourAnt.bandwidthLimit-used)) {
            used += b.length;
            out.write(b);
        } else {
            byte[] array1 = new byte[NeighbourAnt.bandwidthLimit-used];
            byte[] array2 = new byte[b.length-array1.length];

            System.arraycopy(b,0,array1,0,array1.length);
            System.arraycopy(b,array1.length,array2,0,array2.length);

            write(array1);
            write(array2);
        }
    }
    public void flush() throws IOException {
        out.flush();
    }
    public void finalize() throws Throwable{
        //if (controler.isAlive()) controler.stop();
    }
}

